//
//  LocationPicker.swift
//  CommonUI
//
//  Created by sunlubo on 16/7/19.
//  Copyright © 2016年 milanosoft. All rights reserved.
//

import UIKit

/// 位置选择器
public final class LocationPicker: PopupController {
    public var locationPicker: UIPickerView!

    // 初始选中状态
    public var selectedProvinceId = -1
    public var selectedCityId = 0
    public var selectedRegionId = 0

    /// 当前省级行政区
    var currentProvince: Province! {
        didSet {
            currentCity = currentProvince.cities.first!
            locationPicker.reloadComponent(1)
            locationPicker.selectRow(0, inComponent: 1, animated: true)
        }
    }
    /// 当前城市
    var currentCity: City! {
        didSet {
            currentRegion = currentCity.regions.first!
            locationPicker.reloadComponent(2)
            locationPicker.selectRow(0, inComponent: 2, animated: true)
        }
    }
    /// 当前区域
    var currentRegion: Region!

    public override func setupContentView() {
        super.setupContentView()

        title = "选择地址"
        contentHeight = 300
        leftButton.setImage(nil, for: .normal)
        leftButton.setTitle("取消", for: .normal)

        locationPicker = UIPickerView()
        locationPicker.showsSelectionIndicator = true
        locationPicker.dataSource = self
        locationPicker.delegate = self
        contentView.addSubview(locationPicker)
        locationPicker.snp.makeConstraints { $0.edges.equalToSuperview() }

        // 设置初始选中数据
        if selectedProvinceId == -1 {
            currentProvince = provinces.first!
        } else {
            let provinceIndex = provinces.index { $0.id == selectedProvinceId }!
            currentProvince = provinces[provinceIndex]
            locationPicker.selectRow(provinceIndex, inComponent: 0, animated: false)

            let cityIndex = currentProvince.cities.index { $0.id == selectedCityId }!
            currentCity = currentProvince.cities[cityIndex]
            locationPicker.selectRow(cityIndex, inComponent: 1, animated: false)

            let regionIndex = currentCity.regions.index { $0.id == selectedRegionId }!
            currentRegion = currentCity.regions[cityIndex]
            locationPicker.selectRow(regionIndex, inComponent: 2, animated: false)
        }
    }

    public override func handleEventForRightButton() {
        let result = (currentProvince, currentCity, currentRegion)
        self.result = result
    }
}

// MARK: - UIPickerViewDataSource, UIPickerViewDelegate
extension LocationPicker: UIPickerViewDataSource, UIPickerViewDelegate {

    public func numberOfComponents(in _: UIPickerView) -> Int {
        return 3
    }

    public func pickerView(_: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch component {
        case 0: return provinces.count
        case 1: return currentProvince.cities.count
        default: return currentCity.regions.count
        }
    }

    public func pickerView(_: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        var titleLabel: UILabel! = view as? UILabel
        if titleLabel == nil {
            titleLabel = UILabel()
            titleLabel.textAlignment = .center
        }

        switch component {
        case 0: titleLabel.text = provinces[row].name
        case 1: titleLabel.text = currentProvince.cities[row].name
        default: titleLabel.text = currentCity.regions[row].name
        }

        return titleLabel
    }

    public func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        switch component {
        case 0: currentProvince = provinces[row]
        case 1: currentCity = currentProvince.cities[row]
        default: currentRegion = currentCity.regions[row]
        }
    }
}
